/**
 * RmiId.java
 */
package fss.util.rmi;

import java.io.Serializable;

import fss.util.FssConstants;

/**
 * The RMI identification for an object registered in RMI.
 * 
 * @author thinh
 *
 */
public class RmiId implements Serializable {
    private static final long serialVersionUID = 1L;
    /**
     * The host.
     */
    private String host = null;
    /**
     * The name identifier.
     */
    private String name = null;
    /**
     * The RMI port.
     */
    private int port = FssConstants.DEFAULT_RMI_PORT;
    /**
     * Constructor with specified information.
     * @param host the hostname.
     * @param name the object name.
     * @param port the RMI port.
     */
    public RmiId(String host, String name, int port) {
        this(host, name);
        this.port = port;
    }
    /**
     * Constructor using default RMI port.
     * @param host the hostname.
     * @param name the object name.
     */
    public RmiId(String host, String name) {
        this.host = host;
        this.name = name;
    }
    /**
     * Get the hostname.
     * @return the hostname.
     */
    public String getHost() {
        return host;
    }
    /**
     * Get the identifier name.
     * @return the name.
     */
    public String getName() {
        return name;
    }
    /**
     * Get the RMI port.
     * @return the RMI port.
     */
    public int getPort() {
        return port;
    }
    /**
     * Get the fully qualified RMI url.
     * <p>
     * URL format: <code>rmi://host:port/name</code>.
     * @return the url.
     */
    public String getRmiUrl() {
        StringBuilder url = new StringBuilder();
        url.append("rmi://" + host + ":" + port + "/" + name);
        return url.toString();
    }
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((host == null) ? 0 : host.hashCode());
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        result = prime * result + port;
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        RmiId other = (RmiId) obj;
        if (host == null) {
            if (other.host != null)
                return false;
        } else if (!host.equals(other.host))
            return false;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        if (port != other.port)
            return false;
        return true;
    }
    @Override
    public String toString() {
        return name + "@" + host + ":" + port;
    }
}
